package com.fieldchain.ex.fa100.ui.money.activity;

import android.content.Intent;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.KeyEvent;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.fieldchain.ex.fa100.R;
import com.fieldchain.ex.fa100.access.Constant;
import com.fieldchain.ex.fa100.base.MyBaseActivity;
import com.fieldchain.ex.fa100.bean.AddressBean;
import com.fieldchain.ex.fa100.bean.event.CoinEvent;
import com.fieldchain.ex.fa100.ui.money.adapter.AddressAdapter;

import org.greenrobot.eventbus.EventBus;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import com.fieldchain.ex.fa100.utils.net.access.HttpRequest;
import com.fieldchain.ex.fa100.utils.net.modle.ListRequestTwo;

/**
 * Created by biyunlong
 * Email: 1225198434@qq.com
 * Data:  2018/10/22
 */
public class AddressActivity extends MyBaseActivity implements View.OnClickListener {
    private RelativeLayout reaBack;
    private TextView tvCenter;
    private RecyclerView rv;
    private RelativeLayout reaEmpty;
    private TextView tvEmpty;
    private ImageView ivAdd;
    private AddressAdapter adapter;
    private List<AddressBean> mList = new ArrayList<>();
    private String id;
    private String address;


    @Override
    public void findView() {
        reaBack = findViewById(R.id.rea_back);
        tvCenter = findViewById(R.id.tv_center);
        rv = findViewById(R.id.rv);
        reaEmpty = findViewById(R.id.rea_empty);
        tvEmpty = findViewById(R.id.tv_empty);
        ivAdd = findViewById(R.id.iv_add);
    }

    @Override
    protected void onResume() {
        super.onResume();
        getAddress();
    }

    @Override
    public void setValue() {
        id = getIntent().getStringExtra("coinId");
        address=getIntent().getStringExtra("address");
        adapter = new AddressAdapter(mContext);
        rv.setLayoutManager(new LinearLayoutManager(mContext));
        rv.setAdapter(adapter);
        tvCenter.setText("提币地址");
        ivAdd.setOnClickListener(this);
        reaBack.setOnClickListener(this);
    }


    @Override
    public int getLayout() {
        return R.layout.activity_add;
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.iv_add:
                Intent intent=new Intent(mContext,NewAddressActivity.class);
                intent.putExtra("coinId", id);
                startActivity(intent);
                break;
            case R.id.rea_back:
                if(address.length()>0) {
                    EventBus.getDefault().post(new CoinEvent(address, 11));
                }
                finish();
                break;
        }
    }


    public void getAddress() {
        String url = Constant.getaddress + id;
        HashMap map = new HashMap();
        map.put("coinId", id);
        HttpRequest.getInstance().getRequest(map, url,new ListRequestTwo<AddressBean>() {
            @Override
            public void onSucess(List<AddressBean> list, int code, String msg) {
                if (code == 200) {
                    mList.clear();
                    mList.addAll(list);
                    if(address.length()==0&&mList.size()>0){
                        address=mList.get(0).getWalletAddress();
                    }
                    adapter.setData(mList, address);
                    if(mList.size()==0){
                        reaEmpty.setVisibility(View.VISIBLE);
                    }else{
                        reaEmpty.setVisibility(View.GONE);
                    }
                }
            }

            @Override
            public void onFaild(Exception e) {
                reaEmpty.setVisibility(View.VISIBLE);
            }
        });
    }
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if ((keyCode == KeyEvent.KEYCODE_BACK)) {
            if(address.length()>0) {
                EventBus.getDefault().post(new CoinEvent(address, 11));
                finish();
            }
        }
        return true;
    }



}
